package com.phelon.epdm;

import java.math.BigDecimal;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

import org.apache.commons.lang.StringUtils;

import com.phelon.Global.AS400.Factory;

/**
 * @author pdanfy
 */

public class Routing {
	public static void main(String[] args) {
		Routing testRev = new Routing("ASP9802536", "A  ", "      ");
		try {
			testRev.retrieveRecord("7T");
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.exit(0);
	}
	private String site;
	private String routingIdentifier;
	private String routingRevision;
	private String apcRouting;
	private String averageCumulativeYield;
	private String currentCumulativeYield;
	private String description;
	private String flowTimeHours;
	private String longestOperationReciprocal;
	private String longestOperationsTime;
	private String numberOfOperations;
	private String standardCumulativeYield;
	private String totalMoveHours;
	private String totalQueueHours;
	private String totalRunLaborHours;
	private String totalRunMachineHours;
	private String totalSetupContent;
	private String totalSetupDuration;
	private String userFieldDate1;
	private String userFieldSwitchA;
	private String userFieldText40;
	private ArrayList<RoutingOperation>	operations;

	
	public String getAverageCumulativeYield() {
		return averageCumulativeYield;
	}


	public void setAverageCumulativeYield(String averageCumulativeYield) {
		this.averageCumulativeYield = averageCumulativeYield;
	}


	public String getCurrentCumulativeYield() {
		return currentCumulativeYield;
	}


	public void setCurrentCumulativeYield(String currentCumulativeYield) {
		this.currentCumulativeYield = currentCumulativeYield;
	}


	public String getDescription() {
		return description;
	}


	public void setDescription(String description) {
		this.description = description;
	}


	public String getFlowTimeHours() {
		return flowTimeHours;
	}


	public void setFlowTimeHours(String flowTimeHours) {
		this.flowTimeHours = flowTimeHours;
	}


	public String getLongestOperationReciprocal() {
		return longestOperationReciprocal;
	}


	public void setLongestOperationReciprocal(String longestOperationReciprocal) {
		this.longestOperationReciprocal = longestOperationReciprocal;
	}


	public String getLongestOperationsTime() {
		return longestOperationsTime;
	}


	public void setLongestOperationsTime(String longestOperationsTime) {
		this.longestOperationsTime = longestOperationsTime;
	}


	public String getNumberOfOperations() {
		return numberOfOperations;
	}


	public void setNumberOfOperations(String numberOfOperations) {
		this.numberOfOperations = numberOfOperations;
	}


	public String getStandardCumulativeYield() {
		return standardCumulativeYield;
	}


	public void setStandardCumulativeYield(String standardCumulativeYield) {
		this.standardCumulativeYield = standardCumulativeYield;
	}


	public String getTotalMoveHours() {
		return totalMoveHours;
	}


	public void setTotalMoveHours(String totalMoveHours) {
		this.totalMoveHours = totalMoveHours;
	}


	public String getTotalQueueHours() {
		return totalQueueHours;
	}


	public void setTotalQueueHours(String totalQueueHours) {
		this.totalQueueHours = totalQueueHours;
	}


	public String getTotalRunLaborHours() {
		return totalRunLaborHours;
	}


	public void setTotalRunLaborHours(String totalRunLaborHours) {
		this.totalRunLaborHours = totalRunLaborHours;
	}


	public String getTotalRunMachineHours() {
		return totalRunMachineHours;
	}


	public void setTotalRunMachineHours(String totalRunMachineHours) {
		this.totalRunMachineHours = totalRunMachineHours;
	}


	public String getTotalSetupContent() {
		return totalSetupContent;
	}


	public void setTotalSetupContent(String totalSetupContent) {
		this.totalSetupContent = totalSetupContent;
	}


	public String getTotalSetupDuration() {
		return totalSetupDuration;
	}


	public void setTotalSetupDuration(String totalSetupDuration) {
		this.totalSetupDuration = totalSetupDuration;
	}


	public String getUserFieldDate1() {
		return userFieldDate1;
	}


	public void setUserFieldDate1(String userFieldDate1) {
		this.userFieldDate1 = userFieldDate1;
	}


	public String getUserFieldSwitchA() {
		return userFieldSwitchA;
	}


	public void setUserFieldSwitchA(String userFieldSwitchA) {
		this.userFieldSwitchA = userFieldSwitchA;
	}


	public String getUserFieldText40() {
		return userFieldText40;
	}


	public void setUserFieldText40(String userFieldText40) {
		this.userFieldText40 = userFieldText40;
	}


	public Routing(String site, String routingIdentifier, String routingRevision) {
		this.site = site;
		this.routingIdentifier = routingIdentifier;
		this.routingRevision = routingRevision;
	}


	public boolean retrieveRecord(String toEnv) throws SQLException {
		boolean found = false;
		String fileEnv = " ";
		if (toEnv == "7M") { 
			fileEnv = " ";
		} else {
			fileEnv = toEnv.substring(1);
		}
		StringBuilder sql = new StringBuilder("SELECT * FROM AMFLIB")
		.append(fileEnv).append(".RTGHDRL0 WHERE RTIDC3 = ? and " +
				"STIDC3 = ? and RTVRC3 = ?");
		Connection conn = Factory.getConnection();
		PreparedStatement pst = conn.prepareStatement(sql.toString());
		pst.setString(1, routingIdentifier);
		pst.setString(2, site);
		pst.setString(3, routingRevision);
		ResultSet rec = pst.executeQuery();
		if (rec.next()) {
			setSite(rec.getString("STIDC3"));
			setRoutingIdentifier(rec.getString("RTIDC3"));
			setRoutingRevision(rec.getString("RTVRC3"));
			setApcRouting(rec.getString("APCCC3"));
			setAverageCumulativeYield(rec.getString("YCMAC3"));
			setCurrentCumulativeYield(rec.getString("YCMCC3"));
			setDescription(rec.getString("RTHDC3"));
			setFlowTimeHours(rec.getString("TFLOC3"));
			setLongestOperationReciprocal(rec.getString("TLORC3"));
			setLongestOperationsTime(rec.getString("TLOTC3"));
			setNumberOfOperations(rec.getString("NROPC3"));
			setStandardCumulativeYield(rec.getString("YCMSC3"));
			setTotalMoveHours(rec.getString("TMOVC3"));
			setTotalQueueHours(rec.getString("TQUEC3"));
			setTotalRunLaborHours(rec.getString("TRLHC3"));
			setTotalRunMachineHours(rec.getString("TRMHC3"));
			setTotalSetupContent(rec.getString("TSCNC3"));
			setTotalSetupDuration(rec.getString("TSDUC3"));
			setUserFieldDate1(rec.getString("UUD1C3"));
			setUserFieldSwitchA(rec.getString("UUSAC3"));
			setUserFieldText40(rec.getString("UU40C3"));
			found = true;
		}
		rec.close();
		pst.close();
		
		operations = new ArrayList<RoutingOperation>();
		StringBuilder sql2 = new StringBuilder("SELECT * FROM AMFLIB").append(fileEnv)
		.append(".RTGOPRL0 WHERE RTIDC4 = ? and STIDC4 = ? and RTVRC4 = ?");
		PreparedStatement pst2 = conn.prepareStatement(sql2.toString());
		pst2.setString(1, routingIdentifier);
		pst2.setString(2, site);
		pst2.setString(3, routingRevision);
		ResultSet rec2 = pst2.executeQuery();
		while (rec2.next()) {
			RoutingOperation nextOne = new RoutingOperation(routingIdentifier, 
					site, routingRevision);
			nextOne.setOperationSequence(rec2.getString("OPSQC4"));
			nextOne.setAverageOutsideOperationCost(rec2.getString("AOSCC4"));
			nextOne.setCurrentOperationYield(rec2.getString("YCOCC4"));
			nextOne.setDescription(rec2.getString("OPDSC4"));
			nextOne.setFacility(rec2.getString("PFIDC4"));
			nextOne.setInputBufferHours(rec2.getString("BUFIC4"));
			nextOne.setMoveDays(rec2.getString("MVTMC4"));
			nextOne.setNumberOfUnitsAtThisOperation(rec2.getString("PUNTC4"));
			nextOne.setOperationStatus(rec2.getString("OPSTC4"));
			nextOne.setOutputBufferHours(rec2.getString("BUFOC4"));
			nextOne.setOutsideOperationCost(rec2.getString("OSCSC4"));
			nextOne.setProcessSheet(rec2.getString("PRSHC4"));
			nextOne.setReportingPoint(rec2.getString("IRCDC4"));
			nextOne.setResourceConstraintOne(rec2.getString("FCP1C4"));
			nextOne.setResourceContraintTwo(rec2.getString("FCP2C4"));
			nextOne.setRoutingOperationBitmapOrIcon(rec2.getString("BMIDC4"));
			nextOne.setRoutingPrintFlag(rec2.getString("RTPFC4"));
			nextOne.setRunLaborTimeUseTbc(rec2.getString("RLTMC4"));
			nextOne.setRunMachineTimeUseTbc(rec2.getString("RMTMC4"));
			nextOne.setSelectNumber(rec2.getString("SELNC4"));
			nextOne.setSetupCrewSize(rec2.getString("SCSZC4"));
			nextOne.setSetupLaborHours(rec2.getString("SLTMC4"));
			nextOne.setStandardOperationYield(rec2.getString("YCOSC4"));
			nextOne.setTimeBasisCode(rec2.getString("TBCDC4"));
			nextOne.setToolNumber(rec2.getString("TLNOC4"));
			nextOne.setTransactionCode(rec2.getString("TRCDC4"));
			nextOne.setUserFieldCodeA(rec2.getString("UUCAC4"));
			nextOne.setUserFieldCurrencyA(rec2.getString("UUIAC4"));
			nextOne.setUserFieldDate1(rec2.getString("UUD1C4"));
			nextOne.setUserFieldQuantity1(rec2.getString("UUQ1C4"));
			nextOne.setUserFieldSwitchA(rec2.getString("UUSAC4"));
			nextOne.setUserFieldText40(rec2.getString("UU40C4"));
			operations.add(nextOne);
		}
		rec2.close();
		pst2.close();
		
		Factory.freeConnection(conn);
		if (found == true) {
			return true;	
		}
		else {
			return false;			
		}
	}


	public void changeRoutingSite(String site)  { 
		this.setSite(site);
		for (RoutingOperation oper : this.getOperations()) {
			oper.setSite(site);
		}
	}

	public void changeRoutingRevision(String revision)  { 
		this.setRoutingRevision(revision);
		for (RoutingOperation oper : this.getOperations()) {
			oper.setRoutingVersion(revision);
		}
	}

	public String getSite() {
		return site;
	}


	public void setSite(String site) {
		this.site = site;
	}


	public String getRoutingIdentifier() {
		return routingIdentifier;
	}


	public void setRoutingIdentifier(String routingIdentifier) {
		this.routingIdentifier = routingIdentifier;
	}


	public String getRoutingRevision() {
		return routingRevision;
	}


	public void setRoutingRevision(String routingRevision) {
		this.routingRevision = routingRevision;
	}


	public String getApcRouting() {
		return apcRouting;
	}


	public void setApcRouting(String apcRouting) {
		this.apcRouting = apcRouting;
	}


	public ArrayList<RoutingOperation> getOperations() {
		return operations;
	}


	public void setOperations(ArrayList<RoutingOperation> operations) {
		this.operations = operations;
	}

}